import math
import random
import time
import threading
import pyautogui
import win32gui
from win32 import win32api
from yysautomatic.setting import locate_confidence


class Gui(object):
    """
    通过鼠标，键盘操作系统界面
    """

    def find_windows(self, title):
        """
        查找窗口
        :param title: 窗口标题
        :return:
        """

    def get_window_rect(self, win_id):
        """
        获取窗口位置
        :param win_id:
        :return:
        """

    def move_to(self, x, y):
        """
        鼠标移动到指定位置
        :param x: x
        :param y: y
        """
        pass

    def click(self, x, y, hit_count=1):
        """
        鼠标点击
        :param x: 点击位置
        :param y: 点击位置
        :param hit_count: 点击次数
        """
        pass

    def screenshot(self):
        """
        系统截屏
        :return: image
        """
        pass

    def locate(self, image, region=None, grayscale=False):
        """
        图片定位
        :param image: 图片
        :param region: 区域
        :param grayscale: 灰度匹配
        :return:
        """
        pass


class WindowGui(Gui):
    __confidence = 0.9 if locate_confidence is None else locate_confidence

    def __init__(self):
        self.__lock = threading.RLock()

    def find_windows(self, title):
        win_hwnd = 0
        windows = []
        while True:
            win_hwnd = win32gui.FindWindowEx(0, win_hwnd, None, title)
            if win_hwnd > 0:
                windows.append(win_hwnd)
            else:
                return windows
            # left, top, width, height = win32gui.GetWindowRect(win_hwnd)

    def get_window_rect(self, win_id):
        left, top, right, bottom = win32gui.GetWindowRect(win_id)
        screen_width = win32api.GetSystemMetrics(0)
        screen_height = win32api.GetSystemMetrics(1)
        if right > screen_width:
            right = screen_width
        if bottom > screen_height:
            bottom = screen_height
        rect = (left, top, right - left, bottom - top)
        return rect

    def move_to(self, x, y):
        try:
            self.__lock.acquire()
            ox, oy = pyautogui.position()
            if ox == x and oy == y:
                return
            distance = math.sqrt((ox - x) ** 2 + (oy - y) ** 2)
            t = int(distance / 100)
            duration = random.randint(t * 30, (t + 1) * 30)
            pyautogui.moveTo(x=x, y=y, duration=duration / 1000)
        finally:
            self.__lock.release()

    def click(self, x=None, y=None, hit_count=1):
        try:
            self.__lock.acquire()
            self.move_to(x, y)
            for x in range(hit_count):
                t = random.randint(80, 150) / 1000
                time.sleep(t)
                pyautogui.click()
        finally:
            self.__lock.release()

    def screenshot(self):
        return pyautogui.screenshot()

    def locate(self, image, region=None, grayscale=False):
        return pyautogui.locateOnScreen(image, region=region, confidence=self.__confidence, grayscale=False)


gui = WindowGui()
